<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

$goods_cat_viewer = '商品分类列表展示信息';
class b2c_site_goods_list_viewer{

    function __construct($app){
        $this->app = $app;
        $this->db = kernel::database();
    }

    function get_view($cat_id,$view,$type_id=null,$vircat){
            if(!is_array($cat_id)){
                $cat_id=array($cat_id);
         
            }
            $oGtype = $this->app->model('goods_type');
            if($type_id){
                $sqlString = 'SELECT t.schema_id,t.setting,t.type_id FROM sdb_b2c_goods_type t WHERE type_id ='.$type_id;
                $row = $oGtype->dump( $type_id,'schema_id,setting,type_id' );
            }else{
                if($cat_id[0]){
                    $cat_id='('.implode($cat_id,' OR ').')';
                    $sqlString = 'SELECT c.cat_id,c.cat_name,c.tabs,c.addon,t.setting,t.schema_id,t.setting,t.type_id FROM sdb_b2c_goods_cat c
                        LEFT JOIN sdb_b2c_goods_type t ON c.type_id = t.type_id
                        WHERE '.$cat_id;
                }
            }
            if($sqlString) {
                $row = $this->db->selectrow($sqlString);
                $row = array_merge( $row, $oGtype->dump( $row['type_id'],'type_id',array(
                'props'=>array('*',array('props_value'=>array('*',null, array( 0,-1,'order_by ASC' ))),array( 0,-1,'ordernum ASC' ) ) )) );
            }

    //        if($row['spec']) $row['spec'] = unserialize($row['spec']);

            if($row['type_id']){
                if($vircat){ 
                     $cat_id = '('.implode(',',$cat_id).')';
                     $args['cat_id'] = $this->db->select('SELECT type_id FROM sdb_b2c_goods_cat where cat_id in '.$cat_id.'' );
                     foreach($args['cat_id'] as $vk=>$vp){
                          $typeIds[] = $vp['type_id'];
                     }
                    
                     if(is_array($typeIds)){
                         $typeIds = array_unique($typeIds);
                         $typeIds = '('.implode(',',$typeIds).')';
                         $brandIds = $this->db->select('SELECT brand_id FROM sdb_b2c_type_brand where type_id in '.$typeIds.'' );
                     }

                     if(is_array($brandIds)){
                          foreach($brandIds AS $key=>$val){
                              $brands[] = $val['brand_id'];
                          }
                          $row['brand'] = $this->db->select('SELECT b.brand_id,b.brand_name,brand_url,brand_logo FROM sdb_b2c_brand b
                                                             WHERE disabled="false" AND brand_id IN (' . implode(',', $brands) . ')');
                     }else{
                          $row['brand'] = array();
                     }
                }else{
                     $row['brand'] = $this->db->select('SELECT b.brand_id,b.brand_name,brand_url,brand_logo FROM sdb_b2c_type_brand t
                     LEFT JOIN sdb_b2c_brand b ON b.brand_id=t.brand_id
                     WHERE disabled="false" AND t.type_id='.$row['type_id'].' ORDER BY brand_order');
                }
            }else{
                $oBrand = $this->app->model('brand');
                $row['brand'] = $oBrand->getList('*', '', 0, -1);
            }

            $dftList = array(
                    app::get('b2c')->_('列表')=>'list',
                    app::get('b2c')->_('大图')=>'grid',
                    app::get('b2c')->_('文字')=>'text',
                );
            if($row['setting']&&!is_array($row['setting'])){
                $row['setting'] = unserialize($row['setting']);
            }

            if(isset($row['setting']['list_tpl']) && is_array($row['setting']['list_tpl']))
                foreach($row['setting']['list_tpl'] as $k=>$tpl){
                    if(!in_array($tpl,$dftList)){
                        if(!file_exists(SCHEMA_DIR.$row['schema_id'].'/view/'.$tpl.'.html')){
                            unset($row['setting']['list_tpl'][$k]);
                        }
                    }
                }
              
            if(!isset($row['setting']['list_tpl']) || !is_array($row['setting']['list_tpl']) || count($row['setting']['list_tpl'])==0){
                $row['setting']['list_tpl'] = $dftList;
            }
            if($view=='index') $view = current($row['setting']['list_tpl']);

            if(in_array($view,$dftList)){
                    $row['tpl'] = '/site/gallery/type/'.$view.'.html';
            }else{
                $row['tpl'] = realpath(SCHEMA_DIR.$row['schema_id'].'/view/'.$view.'.html');
            }
            $row['dftView'] = $view;
            $row['setting']['list_tpl'][key($row['setting']['list_tpl'])] = 'index';

            return $row;
    }
}
